#Secure Channels Problem Set 4 Task 5
#Jiaqi Weng 115131
#Vasilii Ponteleev 115151
#Naveeni Kumar Goswam 113967

#Compile it with python 3.4.*

#test module


import unittest
from GF import *

class TestPolynomial(unittest.TestCase):

    def test_equal(self):
        polyA = Polynomial(0x34)
        polyB = Polynomial(0x34)
        self.assertTrue(polyA == polyB)

    def test_not_equal(self):
        polyA = Polynomial(0xfd)
        polyB = Polynomial(0x34)
        self.assertTrue(polyA != polyB)

    @unittest.expectedFailure
    def test_should_not_be_negative(self):
        poly = Polynomial(-65)

    def test_bit_representation(self):
        poly = Polynomial(0x53)
        self.assertEqual(poly.asBitListFrom0Bit(),[1, 1, 0, 0, 1, 0, 1])

    def test_should_have_degree_six(self):
        poly = Polynomial(0x53)
        self.assertEqual(poly.degree(), 6)

    def test_should_have_degree_eight(self):
        poly = Polynomial(0x11b)
        self.assertEqual(poly.degree(), 8)   
        

class TestGF256(unittest.TestCase):

    def setUp(self):
        rijndael = Polynomial(0x11b)
        self.field = FiniteFieldBuilder().ofDegree(8).usingIrreduciblePolynomial(rijndael).build()
        self.polyA = Polynomial(0x53)
        self.polyB = Polynomial(0xca)

    def test_addition(self):
        self.assertEqual(self.field.add(self.polyA, self.polyB), Polynomial(0x99))

    def test_substruction(self):
        self.assertEqual(self.field.substruct(self.polyA, self.polyB), Polynomial(0x99))

    def test_multiplication(self):
        self.assertEqual(self.field.mult(self.polyA, self.polyB), Polynomial(0x01))

    def test_inverse(self):
        self.assertEqual(self.field.inverse(self.polyB), Polynomial(0x53))

    def test_division(self):
        self.assertEqual(self.field.divide(self.polyB, self.polyA), Polynomial(0x75))

    @unittest.expectedFailure
    def test_add_arg_one_should_have_degree_no_more_than_7(self):
        self.field.add(Polynomial(0xffff), Polynomial(0xff))

    @unittest.expectedFailure
    def test_add_arg_two_should_have_degree_no_more_than_7(self):
        self.field.add(Polynomial(oxff), Polynomial(0xffff))

    @unittest.expectedFailure
    def test_sub_arg_one_should_have_degree_no_more_than_7(self):
        self.field.substruct(Polynomial(0xffff), Polynomial(0xff))

    @unittest.expectedFailure
    def test_sub_arg_two_should_have_degree_no_more_than_7(self):
        self.field.substruct(Polynomial(oxff), Polynomial(0xffff))

    @unittest.expectedFailure
    def test_mult_arg_one_should_have_degree_no_more_than_7(self):
        self.field.mult(Polynomial(0xffff), Polynomial(0xff))

    @unittest.expectedFailure
    def test_mult_arg_two_should_have_degree_no_more_than_7(self):
        self.field.mult(Polynomial(oxff), Polynomial(0xffff))    

    @unittest.expectedFailure
    def test_divide_arg_one_should_have_degree_no_more_than_7(self):
        self.field.divide(Polynomial(0xffff), Polynomial(0xff))

    @unittest.expectedFailure
    def test_divide_arg_two_should_have_degree_no_more_than_7(self):
        self.field.divide(Polynomial(oxff), Polynomial(0xffff))

    @unittest.expectedFailure
    def test_inverse_arg_should_have_degree_no_more_than_7(self):
        self.field.inverse(Polynomial(oxff1))

class TestGF16(unittest.TestCase):

    def setUp(self):
        poly = Polynomial(0x13)
        self.field = FiniteFieldBuilder().ofDegree(4).usingIrreduciblePolynomial(poly).build()
        self.polyA = Polynomial(0xd)
        self.polyB = Polynomial(0x7)

    def test_addition(self):
        self.assertEqual(self.field.add(self.polyA, self.polyB), Polynomial(0xa))

    def test_substruction(self):
        self.assertEqual(self.field.substruct(self.polyA, self.polyB), Polynomial(0xa))

    def test_multiplication(self):
        self.assertEqual(self.field.mult(self.polyA, self.polyB), Polynomial(0x5))

    def test_inverse(self):
        self.assertEqual(self.field.inverse(self.polyA), Polynomial(0x4))

    def test_division(self):
        self.assertEqual(self.field.divide(self.polyB, self.polyA), Polynomial(0xf))

if __name__ == '__main__':
     unittest.main()




